package com.ruoyi.develop.lvFukuan.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.mapper.GoodsSalebuyMapper;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.mapper.GoodsSalebuyallMapper;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.mapper.LvBuyMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvFukuan.mapper.LvFukuanMapper;
import com.ruoyi.develop.lvFukuan.domain.LvFukuan;
import com.ruoyi.develop.lvFukuan.service.ILvFukuanService;

/**
 * 到货付款Service业务层处理
 * @author qh
 * @date 2024-09-14
 */
@Service
public class LvFukuanServiceImpl implements ILvFukuanService {
    @Resource
    private LvFukuanMapper lvFukuanMapper;
    @Resource
    private LvBuyMapper lvBuyMapper;
    @Resource
    private GoodsSalebuyallMapper goodsSalebuyallMapper;
    @Resource
    private GoodsSalebuyMapper goodsSalebuyMapper;

    /**
     * 查询到货付款
     * @param id 到货付款主键
     * @return 到货付款
     */
    @Override
    public LvFukuan selectById(String id) {
        return lvFukuanMapper.selectById(id);
    }

    /**
     * 查询到货付款列表
     * @param entity 到货付款
     * @return 到货付款
     */
    @Override
    public List<LvFukuan> selectList(LvFukuan entity) {
        return lvFukuanMapper.selectList(entity);
    }

    /**
     * 新增到货付款
     * @param entity 到货付款
     * @return 结果
     */
    @Override
    public int insert(LvFukuan entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
                entity.setCreateBy(SecurityUtils.getUsername());
                entity.setCreateTime(DateUtils.getNowDate());
            return lvFukuanMapper.insert(entity);
    }

    /**
     * 修改到货付款
     * @param entity 到货付款
     * @return 结果
     */
    @Override
    public int update(LvFukuan entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return lvFukuanMapper.update(entity);
    }

    /**
     * 批量删除到货付款
     * @param ids 需要删除的到货付款主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvFukuanMapper.deleteByIds(ids);
    }

    /**
     * 删除到货付款信息
     * @param id 到货付款主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvFukuanMapper.deleteById(id);
    }

    @Override
    public int insertFk(LvFukuan entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setDdSongpeople(SecurityUtils.getUsername());
        entity.setDdDatepeople(DateUtils.getNowDate());
        lvFukuanMapper.insert(entity);

        LvBuy lvBuy=lvBuyMapper.selectById(entity.getCaigouid());
        lvBuy.setFukuan(lvBuy.getFukuan().add(entity.getMoney()));
        lvBuy.setShengyu(lvBuy.getMoney().subtract(lvBuy.getFukuan()));
        List<GoodsSalebuyall> goodsSalebuyalls=goodsSalebuyallMapper.selectMainId(entity.getCaigouid());
        for(GoodsSalebuyall goodsSalebuyall:goodsSalebuyalls){
            if(StringUtils.isNotBlank(goodsSalebuyall.getDakuanId())){
                goodsSalebuyall.setDakuanId(goodsSalebuyall.getDakuanId() + "," + entity.getId());
            }else{
                goodsSalebuyall.setDakuanId(entity.getId());
            }
            if(StringUtils.isNotBlank(goodsSalebuyall.getDakuanno())){
                goodsSalebuyall.setDakuanno(goodsSalebuyall.getDakuanno() + "," + entity.getNos());
            }else{
                goodsSalebuyall.setDakuanno(entity.getNos());
            }
            if(StringUtils.isNotBlank(goodsSalebuyall.getDakuandate())){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                goodsSalebuyall.setDakuandate(goodsSalebuyall.getDakuandate() + "," + formatter.format( entity.getDdDatepeople()));
            } else{
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                goodsSalebuyall.setDakuandate(formatter.format( entity.getDdDatepeople()));
            }
            goodsSalebuyallMapper.update(goodsSalebuyall);
            List<GoodsSalebuy> goodsSalebuyList=goodsSalebuyMapper.selectByCgallId(goodsSalebuyall.getId());
            for(GoodsSalebuy goodsSalebuy:goodsSalebuyList){
                if(StringUtils.isNotBlank(goodsSalebuy.getDakuanId())){
                    goodsSalebuy.setDakuanId(goodsSalebuy.getDakuanId() + "," + entity.getId());
                }else{
                    goodsSalebuy.setDakuanId(entity.getId());
                }
                if(StringUtils.isNotBlank(goodsSalebuy.getDakuanno())){
                    goodsSalebuy.setDakuanno(goodsSalebuy.getDakuanno() + "," + entity.getNos());
                }else{
                    goodsSalebuy.setDakuanno(entity.getNos());
                }
                if(StringUtils.isNotBlank(goodsSalebuy.getDakuandate())){
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    goodsSalebuy.setDakuandate(goodsSalebuy.getDakuandate() + "," + formatter.format( entity.getDdDatepeople()));
                } else{
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    goodsSalebuy.setDakuandate(formatter.format( entity.getDdDatepeople()));
                }
                goodsSalebuyMapper.update(goodsSalebuy);
            }
        }
        return lvBuyMapper.update(lvBuy);
    }
}
